﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Runtime.InteropServices;
using System.IO;

namespace Velocity.Ink
{
    internal class Internals
    {

        static public Bitmap LoadImage(string imageName)
        {
            return new Bitmap(System.Reflection.Assembly.GetExecutingAssembly().
                GetManifestResourceStream("Velocity.Ink.Resources." + imageName));
        }

        /// <summary>
        /// Write int bytes to byte stream.
        /// </summary>
        static public void WriteInt32(MemoryStream stream, Int32 data)
        {
            stream.Write(BitConverter.GetBytes(data), 0, Marshal.SizeOf(data));
        }

        /// <summary>
        /// Write string to byte stream. First write length, followed
        /// by string content.
        /// </summary>
        static public void WriteString(MemoryStream stream, string data)
        {
            // write length of string followed by the string bytes
            WriteInt32(stream, data.Length);
            stream.Write(ASCIIEncoding.ASCII.GetBytes(data), 0, data.Length);
        }

        /// <summary>
        /// Return int from byte stream.
        /// </summary>
        static public Int32 GetInt32(byte[] bits, ref int bitsIndex)
        {
            Int32 data = BitConverter.ToInt32(bits, bitsIndex);
            bitsIndex += Marshal.SizeOf(data);
            return data;
        }

        /// <summary>
        /// Return specified bytes from byte stream.
        /// </summary>
        static public byte[] GetBytes(byte[] bits, ref int bitsIndex, int length)
        {
            byte[] data = new byte[length];
            Buffer.BlockCopy(bits, bitsIndex, data, 0, length);
            bitsIndex += length;
            return data;
        }

    }
}
